package com.bing.habit.controller.user;

import com.bing.habit.entity.ClockinLog;
import com.bing.habit.model.result.Result;
import com.bing.habit.model.result.ResultFactory;
import com.bing.habit.service.ClockInLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 用户对打卡日志的操作
 */
@RestController
@RequestMapping("/clockin_Log")
public class ClockInLogController {

    @Autowired
    ClockInLogService clockinLogService;

    /**
     * 增加/更新 打卡日志
     *
     * @param
     * @return
     */
    @PostMapping
    public Result addOrUpdate(@RequestBody ClockinLog clockinLog) {
        clockinLog.setCreateTime(new Date());
        clockinLogService.addOrUpdate(clockinLog);
        return ResultFactory.buildSuccessResult("添加日志成功");
    }

    /**
     * 删除打卡日志
     */
    @DeleteMapping("/{clockinLogId}")
    public Result delete(@PathVariable("clockinLogId") int clockinLogId) {
        clockinLogService.delete(clockinLogId);
        return ResultFactory.buildSuccessResult("删除日志成功");
    }

    @DeleteMapping("/habit/{habitId}")
    public Result deleteByHid(@PathVariable("habitId") int habitId) {
        clockinLogService.deleteByHidAndToday(habitId);
        return ResultFactory.buildSuccessResult("删除日志成功");
    }

    @GetMapping("/detail/{logId}")
    public Result get(@PathVariable("logId") int logId) {
        return ResultFactory.buildSuccessResult(clockinLogService.get(logId));
    }

    /**
     * 根据habitId列出打卡记录时间集合
     *
     * @param habitId
     * @return
     */
    @GetMapping("/{habitId}")
    public Result list(@PathVariable("habitId") int habitId) {
        List<ClockinLog> clockinLogs = clockinLogService.listByHabit(habitId);
        return ResultFactory.buildSuccessResult(clockinLogs);
    }

    @GetMapping("/toadyLog/{habitId}")
    public Result getTodayLog(@PathVariable("habitId") int habitId) {
        return ResultFactory.buildSuccessResult(clockinLogService.listByToday(habitId));
    }

    /**
     * @description: 上传文件
     * @author: xujiabing
     * @date: 2024-09-28 15:22
     * @param: file
     * @return: java.lang.String
     */
    @PostMapping("/uploadImage")
    public String upload(MultipartFile file) throws Exception {
        return clockinLogService.uploadFile(file);
    }
}
